package cn.edu.pzhu.javaweb.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.edu.pzhu.javaweb.dao.UserinfoDao;
import cn.edu.pzhu.javaweb.pojo.Userinfo;
import cn.edu.pzhu.javaweb.util.DBUtil;
import cn.edu.pzhu.javaweb.util.DruidUtils;

public class UserinfoDaoimp implements UserinfoDao{
	
	private JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
	public UserinfoDaoimp() {
		// TODO Auto-generated constructor stub
	}

	@Override
public int deleteById(String id) {
	String sql = "delete from userinfo where username=?";
	return template.update(sql, id);
}

	@Override
	public int update(Userinfo entity) {
		String sql = "update userinfo set email=?,gender=?,type=?where username=?";
		return template.update(sql,entity.getEmail(),entity.getGender(),entity.getType(),entity.getUsername());
	}

	@Override
	public Userinfo selectById(String id) {
		String sql = "select * from userinfo where username=?";
		try {
			return template.queryForObject(sql,new BeanPropertyRowMapper<>(Userinfo.class),id);
		}catch(Exception e) {
			return null;
		}
	}

	@Override
	public List<Userinfo> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int batchInsert(List<Userinfo> entities) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int batchDelete(List<String> ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(Userinfo entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(Userinfo entity, Connection con) {
		PreparedStatement pstm = null;
	    try {
	        String sql = "insert into userinfo values(?,?,?,?)";
	        pstm = con.prepareStatement(sql);
	        pstm.setString(1, entity.getUsername());
	        pstm.setString(2, entity.getEmail());
	        pstm.setInt(3, entity.getGender());
	        pstm.setString(4, entity.getType());
	        int n = pstm.executeUpdate();
	        return n;
	    } catch (SQLException e) {
	        e.printStackTrace();
	        return 0;
	    } finally {
	        DBUtil.close(pstm);
	    }
	}

}
